package com.youmu.shop.user.service.impl;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.youmu.shop.coupon.bean.Coupon;
import com.youmu.shop.coupon.dao.CouponDao;
import com.youmu.shop.system.bean.MemberRank;
import com.youmu.shop.system.vo.UserInfo;
import com.youmu.shop.user.dao.UserDao;
import com.youmu.shop.user.service.UserService;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserDao userDao;

	@Autowired
	private CouponDao couponDao;
	
	@Override
	public UserInfo findUserInfo(Long userId) {
		UserInfo userInfo = userDao.findUserInfo(userId);
	    MemberRank memberRank = userDao.selectNextMemberRank(userDao.selectMemberRankById(Long.parseLong(userInfo.getMemberRank())).getAmount());
	    userInfo.setNextPoint(memberRank.getAmount().toString());
	    return userInfo;
	}

    /** 
     * @see com.youmu.shop.user.service.UserService#saveUserInfo(com.youmu.shop.system.vo.UserInfo)
     */
    @Override
    public void saveUserInfo(UserInfo userInfo) {
        userDao.saveUserInfo(userInfo);
    }

    /** 
     * @see com.youmu.shop.user.service.UserService#findUserInfoByOpenId(java.lang.String)
     */
    @Override
    public UserInfo findUserInfoByOpenId(String openId) {
        return userDao.findUserInfoByOpenId(openId);
    }

	@Override
	public List<Coupon> checkUserHasNewCoupon(Long userId) {
		MemberRank memberRank = userDao.selectMemberRankByUserId(userId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("memberRankId", memberRank.getScale());
		map.put("endTime", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
		return couponDao.selectCouponsByUserId(map);
	}

	@Override
	public MemberRank selectMemberRankById(Long id) {
		return userDao.selectMemberRankById(id);
	}

	@Override
	public MemberRank selectMemberRankByGrade(Integer grade) {
		return userDao.selectMemberRankByGrade(grade);
	}
}
